﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Conqueror.API
{
    [XmlRoot("api")]
    public class APIMapResponse
    {
        [XmlElement("maps")]
        public Maps Content { get; set; }
    }

    public class Maps
    {
        [XmlElement("map")]
        public List<Map> MapList { get; set; }
    }

    //"N" = Normal, "B" = Beta, "C" = Closed, "R" = Random
    public enum MapStatus
    {
        [XmlEnum("N")]
        Normal,
        [XmlEnum("B")]
        Beta,
        [XmlEnum("C")]
        Closed,
        [XmlEnum("R")]
        Random
    }

    public class Map
    {
        private const string mode = "maplist";
        public static async Task<List<Map>> GetMapAsync(string map = null)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(GenerateURI(map));
            WebResponse response = await request.GetResponseAsync();

            //Error checks
            if (response.ContentType != "application/xml")
                throw new FormatException("The response is not XML");

            XmlSerializer serializer = new XmlSerializer(typeof(APIMapResponse));
            return await Task.Run(() =>
            {
                var result = (APIMapResponse)serializer.Deserialize(response.GetResponseStream());
                return result.Content.MapList;
            });
        }

        private static Uri GenerateURI(string map = null)
        {
            if (string.IsNullOrWhiteSpace(map))
                return new Uri(string.Format("{0}?mode={1}", API.APIAddress, mode));
            else return new Uri(string.Format("{0}?mode={1}&mp={2}", API.APIAddress, mode, map));
        }

        private const string linkedImage = @"http://maps.conquerclub.com/";
        private const string xmlLink = @"http://conquerclub.com/maps/";

        [XmlElement("title")]
        public string Title { get; set; }
        [XmlElement("xml")]
        public string XML { get; set; }
        [XmlIgnore()]
        public string XMLLink { get { return xmlLink + XML; } }
        [XmlElement("small")]
        public string Small { get; set; }
        [XmlIgnore()]
        public string SmallImage { get { return linkedImage + Small; } }
        [XmlElement("large")]
        public string Large { get; set; }
        [XmlIgnore()]
        public string LargeImage { get { return linkedImage + Large; } }
        [XmlElement("thumbnail")]
        public string Thumbnail { get; set; }
        [XmlIgnore()]
        public string ThumbnailImage { get { return linkedImage + Thumbnail; } }
        [XmlElement("topic")]
        public int Topic { get; set; }
        [XmlElement("status")]
        public MapStatus Status { get; set; }
        [XmlElement("small_width")]
        public int SmallWidth { get; set; }
        [XmlElement("small_height")]
        public int SmallHeight { get; set; }
        [XmlElement("large_width")]
        public int LargeWidth {get; set;}
        [XmlElement("large_height")]
        public int LargeHeight { get; set; }
        [XmlElement("territories")]
        public int Territories { get; set; }
    }
}
